﻿Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtWatch.Text = My.Settings.WatchDir
        txtbackup.Text = My.Settings.BackupDir
        FileSystemWatcher1.Path = My.Settings.WatchDir
        My.Settings.Reload()
        LogMsg("Settings Restored")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please select directory."
        theFolderBrowser.ShowNewFolderButton = False
        theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
        theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
        If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtWatch.Text = theFolderBrowser.SelectedPath
            FileSystemWatcher1.Path = theFolderBrowser.SelectedPath
            My.Settings.WatchDir = theFolderBrowser.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim theFolderBrowser As New FolderBrowserDialog
        theFolderBrowser.Description = "Please select directory."
        theFolderBrowser.ShowNewFolderButton = False
        theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
        theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop
        If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtbackup.Text = theFolderBrowser.SelectedPath
            My.Settings.BackupDir = theFolderBrowser.SelectedPath
        End If
    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        My.Settings.WatchDir = txtWatch.Text
        My.Settings.BackupDir = txtbackup.Text
        My.Settings.Save()

        e.Cancel = True
        Me.Visible = False
    End Sub

    Sub LogMsg(msg As String)

        RichTextBox1.AppendText("[" & Now.Hour & ":" & Now.Minute & "." & Now.Second & "] : " & msg & vbCrLf)
    End Sub


    Private Sub WatchTime_Tick(sender As Object, e As EventArgs) Handles WatchTime.Tick

    End Sub

    Private Sub FileSystemWatcher1_Changed_1(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Dim file As New FileInfo(e.FullPath)
        LogMsg("Changed : " & file.FullName)
        ImmidieteCopy(file, txtbackup.Text, 1)
    End Sub

    Private Sub FileSystemWatcher1_Created(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
        Dim file As New FileInfo(e.FullPath)
        LogMsg("Created : " & file.FullName)
        ImmidieteCopy(file, txtbackup.Text, 2)
    End Sub
    Private Sub FileSystemWatcher1_Deleted(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted
        Dim file As New FileInfo(e.FullPath)
        LogMsg("Delete : " & file.FullName)
        ImmidieteCopy(file, txtbackup.Text, 3)
    End Sub

    Private Sub FileSystemWatcher1_Renamed(sender As Object, e As IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
        Dim file As New FileInfo(e.FullPath)
        LogMsg("Renamed : " & file.FullName)
        ImmidieteCopy(file, txtbackup.Text, 4)
    End Sub

    'Copy Changes to Backup
    Public Function ImmidieteCopy(Filea As FileInfo, BackDir As String, method As Integer) As Boolean
        'Get Subdirectory and File by Replacing Path and File string
        'Dim WatchSub As New IO.DirectoryInfo(txtWatch.Text)
        'For Each fileObject In WatchSub.GetFileSystemInfos
        'MsgBox(fileObject.FullName)
        'Next
        'Directory.GetDirectories(txtWatch.Text)

        For Each a In Directory.GetDirectories(txtWatch.Text)
            Dim subcat As String
            subcat = a.Replace(txtWatch.Text, "")
            Directory.CreateDirectory(txtbackup.Text & "\" & subcat)
        Next




        Dim Filename As String = Filea.FullName
        Dim ReplaceOllNew As String = Filename.Replace(txtWatch.Text, "")

        '1 change
        '2 created
        '3 delete
        '4 rename
        Try

        
            If method = 1 Then
                If File.Exists(txtbackup.Text & ReplaceOllNew) Then
                    File.Replace(Filea.FullName, txtbackup.Text & ReplaceOllNew & ".tmp", txtbackup.Text & ReplaceOllNew)
                    If File.Exists(txtbackup.Text & ReplaceOllNew & ".tmp") Then
                        File.Delete(txtbackup.Text & ReplaceOllNew & ".tmp")
                    End If
                Else
                    File.Copy(Filea.FullName, txtbackup.Text & ReplaceOllNew, True)
                End If
            ElseIf method = 2 Then
                If File.Exists(txtbackup.Text & ReplaceOllNew) Then
                    File.Replace(Filea.FullName, txtbackup.Text & ReplaceOllNew & ".tmp", txtbackup.Text & ReplaceOllNew)
                    If File.Exists(txtbackup.Text & ReplaceOllNew & ".tmp") Then
                        File.Delete(txtbackup.Text & ReplaceOllNew & ".tmp")
                    End If
                Else
                    File.Copy(Filea.FullName, txtbackup.Text & ReplaceOllNew, True)
                End If
                Return True
            ElseIf method = 3 Then
                If File.Exists(txtbackup.Text & ReplaceOllNew) Then
                    File.Delete(txtbackup.Text & ReplaceOllNew)
                End If
                Return True
            ElseIf method = 4 Then
                File.Copy(Filea.FullName, txtbackup.Text & ReplaceOllNew)
                'compare
                Dim dirInfo As New IO.DirectoryInfo(txtWatch.Text)
                Dim fileObject As FileSystemInfo
                For Each fileObject In dirInfo.GetFileSystemInfos
                    MsgBox(fileObject.FullName)

                Next
                Return True
            End If


        Catch ex As Exception
            If ex.ToString.Contains("is a directory, not a file") Then
            Else

            End If
            Return False
        End Try
        Return False
    End Function



    Private Sub NotifyIcon1_MouseClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseClick
        Me.Visible = True
    End Sub
End Class
